package com.tengju.data.infrastructure.repository;

import com.tengju.data.domain.model.activity.ActivityDetail;
import com.tengju.data.domain.model.activity.ActivityId;
import com.tengju.data.domain.model.activity.ActivityTargetRepository;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.DealerMarketingCalendarDO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.data.custom.DealerMarketingCalendarMapper;
import com.tengju.data.infrastructure.repository.convert.ActivityTargetAnalysisConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: 大木
 * @date: 2021/10/13 7:53 下午
 */
@Repository
@RequiredArgsConstructor(onConstructor_ ={@Autowired})
public class ActivityTargetRepositoryImpl implements ActivityTargetRepository {

    private final DealerMarketingCalendarMapper dealerMarketingCalendarMapper;

    @Value("#{${activity.target.config}}")
    private Map<String,Object> activityConfig;

    @Override
    public List<ActivityDetail> getActivityInfo(LocalDate startTime, LocalDate endTime) {
        if (startTime == null || endTime == null){
            return Collections.emptyList();
        }
        List<DealerMarketingCalendarDO> activityInfo = dealerMarketingCalendarMapper.getActivityByPeriod(startTime, endTime);

        List<ActivityDetail> details = activityInfo.stream().map(ActivityTargetAnalysisConvert::convert).collect(Collectors.toList());

        if (LocalDate.parse((String) activityConfig.get("startTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd")).isBefore(endTime) &&
                LocalDate.parse((String) activityConfig.get("endTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd")).isAfter(startTime)) {

            details.stream()
                    .filter(detail -> detail.getActivityId().equals(new ActivityId((Integer) activityConfig.get("activityId"))))
                    .findFirst()
                    .ifPresent(details::remove);

            details.add(new ActivityDetail(new ActivityId((Integer) activityConfig.get("activityId")),
                    (String) activityConfig.get("activityName"),
                    LocalDate.parse((String) activityConfig.get("startTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd")),
                    LocalDate.parse((String) activityConfig.get("endTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd"))));
        }

        return details;
    }

    @Override
    public ActivityDetail getActivityById(ActivityId activityId) {

        if (activityId.equals(new ActivityId((Integer) activityConfig.get("activityId")))) {
            return new ActivityDetail(new ActivityId((Integer) activityConfig.get("activityId")),
                    (String) activityConfig.get("activityName"),
                    LocalDate.parse((String) activityConfig.get("startTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd")),
                    LocalDate.parse((String) activityConfig.get("endTime"), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }

        DealerMarketingCalendarDO activity =
                dealerMarketingCalendarMapper.getActivityById(activityId.getValue());
        return ActivityTargetAnalysisConvert.convert(activity);
    }

}
